package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.competition.entity.Product;
import com.competition.mapper.ProductMapper;
import com.competition.resp.ResponseResult;
import com.competition.service.ProductService;
import com.upyun.UpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    CommonController commonController;

    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/queryProductList")
    public ResponseResult<Object> queryProductList(@RequestParam HashMap<String,Object> params) {
        if(params.get("page")==null||params.get("pageSize")==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        QueryWrapper<Product> objectQueryWrapper = new QueryWrapper<>();
        int page = Integer.parseInt(params.get("page").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        if(params.get("productName")!=null){
            objectQueryWrapper.like("product_name",params.get("productName").toString());
        }
        objectQueryWrapper.last("limit "+pageSize+" offset "+(page - 1) * pageSize);
        List<Product> productList = productService.list(objectQueryWrapper);
        HashMap<String, Object> resMap = new HashMap<>();
        long count = productService.count();
        resMap.put("total",count);
        resMap.put("productList",productList);
        return new ResponseResult<Object>(200,"获取商品列表成功",resMap);
    }


    @GetMapping("/queryProductById")
    public ResponseResult<Object> queryProductById(@RequestParam HashMap<String,Object> params) {
        if(params.get("productId")==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        Product product = productService.getById(params.get("productId").toString());
        if(product==null){
            return new ResponseResult<Object>(400,"商品不存在",null);
        }
        return new ResponseResult<Object>(200,"获取商品成功",product);
    }

    @PutMapping
    public ResponseResult<Object> modifyProduct(@RequestBody Product product) {
        if (product.getProductId() == null) {
            return new ResponseResult<Object>(400, "参数错误", null);
        }
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("product_id", product.getProductId());
        // 更新商品信息
        productService.update(product, productQueryWrapper);
        return new ResponseResult<Object>(200, "商品信息更新成功", null);
    }

    @DeleteMapping("/deleteProductById")
    public ResponseResult<Object> deleteProductById(@RequestParam HashMap<String,Object> params) {
        if(params.get("productId")==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("product_id", params.get("productId").toString());
        // 删除商品信息
        productService.remove(productQueryWrapper);
        return new ResponseResult<Object>(200,"删除商品成功",null);
    }

    @PostMapping("/addProduct")
    public ResponseResult<Object> addProduct(Product product,@RequestPart(value = "file") MultipartFile file) throws UpException, IOException {
        if(file != null){
            String filename = file.getOriginalFilename();
            assert filename != null;
            int dotIndex = filename.lastIndexOf('.');
            String suffix = filename.substring(dotIndex + 1);
            ResponseResult responseResult = commonController.uploadImg(file, "product", suffix);
            if(responseResult.getCode()!=200){
                return new ResponseResult<Object>(500,"文件上传失败");
            }
            product.setProductImg(responseResult.getData().toString());
        }
        productService.save(product);
        return new ResponseResult<Object>(200,"添加商品成功",null);
    }

    @GetMapping("/getProduct")
    public ResponseResult getProduct(@RequestParam("current")int current){
        if (current < 0){
            return new ResponseResult(400,"参数异常",null);
        }
        try {
            Page<Product> productPage = productMapper.selectPage(new Page<>(current, 5), null);
            return new ResponseResult(200,"获取成功",productPage);
        } catch (Exception e) {
            return new ResponseResult(400,"获取失败",null);
        }
    }
    @GetMapping("/getProductDetail")
    public ResponseResult getProductDetail(@RequestParam("productId")String productId ){
        if (productId == null){
            return new ResponseResult(400,"参数异常",null);
        }
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("product_id",productId);
            Product product = productMapper.selectOne(queryWrapper);
            return new ResponseResult(200,"获取成功",product);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400,"获取失败",null);
        }
    }
    @GetMapping("/getProductDetailByValue")//搜索
    public ResponseResult getProductDetailByValue(@RequestParam("productLable")String productLable){
        //后续可换高级的搜索 建索引提高查询效率 加缓存 或者Solr ，elasticsearch
        if (productLable == null){
            return new ResponseResult(400,"参数异常",null);
        }
        try {

            List<Product> allByProductLable = productMapper.getAllByProductLable(productLable+"%");
            return new ResponseResult(200,"获取成功",allByProductLable);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400,"获取失败",null);
        }
    }

}
